/**
 * 
 */
package redsocial.semaforo.interfaz;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

/**
 * @author JSR
 *
 */
public class HelloItemizedOverlay extends ItemizedOverlay
{
    
    // ATRIBUTOS
    private ArrayList<OverlayItem> overlays=new ArrayList<OverlayItem>( );
    
    private Context papa;
    
    /**
     * @param defaultMarker
     */
    public HelloItemizedOverlay( Drawable defaultMarker, Context padre )
    {
      
        super( boundCenterBottom(defaultMarker) );
        papa = padre;
        // TODO Auto-generated constructor stub
    }
    
    public void addOverlay(OverlayItem overlay)
    {
        overlays.add( overlay );
        setLastFocusedIndex( -1 );
        populate();
    }
    
    public void clear(){
        overlays.clear( );
        setLastFocusedIndex(-1);
        populate();
    }

    /* (non-Javadoc)
     * @see com.google.android.maps.ItemizedOverlay#createItem(int)
     */
    @Override
    protected OverlayItem createItem( int arg0 )
    {
        // TODO Auto-generated method stub
        return overlays.get( arg0 );
    }

    /* (non-Javadoc)
     * @see com.google.android.maps.ItemizedOverlay#size()
     */
    @Override
    public int size( )
    {
        // TODO Auto-generated method stub
        return overlays.size( );
    }
   @Override
    public boolean onTouchEvent(MotionEvent event, MapView mapView) 
    {   
        //---when user lifts his finger---
        if (event.getAction() == MotionEvent.ACTION_UP) {                
            GeoPoint p = mapView.getProjection().fromPixels(
                (int) event.getX(),
                (int) event.getY());
            AlertDialog.Builder dialog = new AlertDialog.Builder( papa );
            dialog.setTitle(  p.getLatitudeE6() / 1E6 + "," + 
                    p.getLongitudeE6() /1E6 );
            
            dialog.show( );
            
        }                            
        return false;
    }        

    /**
     * @return
     */
    private Context getBaseContext( )
    {
        // TODO Auto-generated method stub
        return null;
    }

        
    @Override
    protected boolean onTap(int index) {
        OverlayItem item= overlays.get(index);
        AlertDialog.Builder dialog = new AlertDialog.Builder( papa );
        dialog.setTitle( item.getTitle() );
        dialog.setMessage( item.getSnippet( ) );
        dialog.show( );
        return super.onTap( index );
    }

    @Override
    public boolean onTap(GeoPoint p, MapView mapView) {
        return super.onTap( p, mapView );
    }

}
